package top.jle.jpa.basic.entities;

import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.MappedSuperclass;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.GenericGenerator;
import top.jle.jpa.basic.dao.GUIDGenerator;

import java.io.Serial;

/**
 * @description 处理所有实体的ID生成
 * @date  2023/7/12 下午3:11
 * @author  jim.lee
 */
@Setter
@Getter
@MappedSuperclass
public abstract class BaseEntity implements Entity{
	/**
	 * 
	 */
	@Serial
	private static final long serialVersionUID = 3612865285497138964L;
	@Id
	@GenericGenerator(name ="idGenerator", type = GUIDGenerator.class)
	@GeneratedValue(generator ="idGenerator",strategy = GenerationType.AUTO)
	private Long id;

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		BaseEntity other = (BaseEntity) obj;
		if (id == null) {
			return false;
		} else return id.equals(other.id);
	}
	
}
